
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordStoreException;

public class AddScore extends Form implements CommandListener {

    private Main iMain;
    private MenuScreen iMenuScreen;
    private HighScoreScreen iHighScoreScreen;
    private TextField nickName;
    private TextField score;
    private Command cmdBack;
    private Command cmdOk;
    private Player clickSound;

    public AddScore(Main main, String score, MenuScreen menu) {
        super("New high score: ");
        nickName = new TextField("Nick name: ", null, 12, TextField.ANY);
        this.score = new TextField("Score: ", score, 15, TextField.BUTTON);
        append(nickName);
        append(this.score);

        iMain = main;
        iMenuScreen = menu;

        cmdOk = new Command("OK", Command.OK, 0);
        addCommand(cmdOk);
        setCommandListener(this);
        this.iMain = main;
        try {
            //moveSound = Manager.c
            InputStream in2 = getClass().getResourceAsStream("/click.wav");
            clickSound = Manager.createPlayer(in2, "audio/x-wav");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    public void commandAction(Command command, Displayable d) {
        if (command == cmdOk) {
            if (OptionScreen.getChon() == 1) {
                try {
                    clickSound.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
            if (!"".equals(nickName.getString().toString()) && !isChuoiTrang()) {
                try {
                    int SaveAdd = this.iMain.addScore(nickName.getString(), score.getString());
                    iHighScoreScreen = new HighScoreScreen(iMain, iMenuScreen);
                    iHighScoreScreen.Start();
                    Display.getDisplay(iMain).setCurrent(iHighScoreScreen);
                } catch (IOException ex) {
                } catch (RecordStoreException ex) {
                }

            }
        }

    }

    private boolean isChuoiTrang() {
        String st = nickName.getString();

        if (!" ".equals(st.substring(0, 1))) {
            return false;
        }

        return true;
    }
}
